package com.village.zpark.config;


import com.village.zpark.interceptors.LoginHandlerInterceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

//配置类
@Configuration
public class MySpringMvcConfig implements WebMvcConfigurer{
    //添加静态资源，外部可以直接访问
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        //第一个参数：访问路径，对外暴露访问路径
        //第二个参数：资源路径，是内部文件放置的路径，不能是外网
        registry.addResourceHandler("/").addResourceLocations("/login");
        registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");
        WebMvcConfigurer.super.addResourceHandlers(registry);
    }

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        //第一个参数:路径
        //第二个参数:跳转页面名称，不需要写后缀名
        registry.addViewController("/").setViewName("login");
        registry.addViewController("/index.html").setViewName("index");
        registry.addViewController("/index").setViewName("index");
        registry.addViewController("/login.html").setViewName("login");
        registry.addViewController("/index1.html").setViewName("index1");
        registry.addViewController("/index1").setViewName("index1");
        registry.addViewController("/register.html").setViewName("register");
        registry.addViewController("/register").setViewName("register");

    }

    //配置自定义拦截器
    //addInterceptor方法：配置自定义拦截器，需要一个实现HandlerInterceptor接口的拦截器实例（创建对象）
    //addPathPatterns方法：拦截相关请求，用于设置拦截器的过滤路径规则
    //excludePathPatterns方法：排除拦截相关请求，用于设置不需要拦截的过滤路径规则
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new LoginHandlerInterceptor()).addPathPatterns("/**").excludePathPatterns(
                "/login.html",
                "/index",
                "/user/login",
                "/static/**",
                "/",
                "/admin/login",
                "/admin/logout",
                "/register"
        );

    }
}
